package ui.fragment.minefragment;

import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;

import org.angmarch.views.NiceSpinner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import adapter.MyClassTimeAdapter;
import base.BaseBean;
import base.fragment.BaseNetFragment;
import bean.ClassTimeBean;
import student.andlei.com.student.R;
import ui.fragment.MineFragment;
import utils.GsonUtils;
import utils.TimerUtil;

/**
 * 我的课时
 */
public class MyClassTimeFragment extends BaseNetFragment{
    private TextView tv_spare_time,
                     tv_hasbuy_time,
                     tv_recharge,
                     tv_use;
    private Button bt_buy;
    private NiceSpinner spinner_type,spinner_date;
    private RecyclerView recyclerView;
    private List<ClassTimeBean.mlist> lists = new ArrayList<>();
    private MyClassTimeAdapter adapter;
    private ClassTimeBean classTimeBean;
    private int type;
    private String date;
    public static Fragment getInstance() {
        return new MyClassTimeFragment();
    }
    @Override
    public void success(String action, BaseBean baseBean) {
         if(baseBean.code == 200){
             classTimeBean = (ClassTimeBean) GsonUtils.getInstance(context).parseComplexJArrayByCommon(baseBean,ClassTimeBean.class);
             tv_spare_time.setText("剩余课时："+classTimeBean.amount+" 课时");
             tv_hasbuy_time.setText("已买课时："+classTimeBean.bought+" 课时");
             tv_recharge.setText("充值："+classTimeBean.recharge+" 课时");
             tv_use.setText("消耗： "+classTimeBean.expend+" 课时");
             lists = classTimeBean.list;
             adapter.setNewData(lists);
         }else {
             toast(baseBean.msg);
         }

    }

    @Override
    public void error(String action, Throwable e) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_myclasstime;
    }

    @Override
    protected void findViews() {
        tv_spare_time = getView(R.id.tv_myclasstime_myclasstimenumber);
        tv_hasbuy_time = getView(R.id.tv_myclasstime_mypointdetail);
        tv_recharge = getView(R.id.tv_recharge);
        tv_use = getView(R.id.tv_use);
        spinner_date = getView(R.id.spinner_time);
        spinner_type = getView(R.id.spinner_state);
        bt_buy = getView(R.id.bt_buy);
        recyclerView = getView(R.id.ry_my);
    }

    @Override
    protected void formatViews() {
        bt_buy.setOnClickListener(this);
        List<String> list_type = new ArrayList<>();
        //0:全部 10:充值 20:消耗
        list_type.add("全部");
        list_type.add("充值");
        list_type.add("消耗");
        spinner_type.attachDataSource(list_type);
        //类型选择
        spinner_type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                TextView textView = (TextView) view;
                if(textView.getText().toString().equals("全部")){
                    type = 0;
                }else if(textView.getText().toString().equals("充值")){
                    type = 10;
                }else if(textView.getText().toString().equals("消耗")){
                    type = 20;
                }
                loadData();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        spinner_date.attachDataSource(TimerUtil.GetlistDate());

        //日期选择
        spinner_date.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                TextView textView = (TextView) view;
                date = textView.getText().toString();
                loadData();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    @Override
    protected void formatData() {
//        for (int i=0;i<10;i++){
//            MyPointDetailBean bean = new MyPointDetailBean();
//            bean.setTime("2018-8-"+i);
//            bean.setTitle("答题"+i);
//            bean.setPoints("+11");
//            lists.add(bean);
//        }
        //创建布局管理
        LinearLayoutManager layoutManager = new LinearLayoutManager(this.context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

        //创建适配器
        adapter = new MyClassTimeAdapter(R.layout.item_my_pointdetail,lists);

        //给RecyclerView设置适配器
        recyclerView.setAdapter(adapter);
        date = TimerUtil.GetlistDate().get(0);
        loadData();
    }
    private void loadData(){
        Map<String,String> map = new HashMap<>();
        map.put("type",type+"");//0:全部 10:充值 20:消耗
        map.put("date",date);//月份 2018-08
        post("personal/timeList",map,ClassTimeBean.class,false);

    }
    @Override
    protected void getBundle() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt_buy:
                ((MineFragment)(this.getParentFragment())).GojumpBuyClassTimeFragment();
                break;
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
            loadData();
        }
    }
}
